package com.xmy.jdbcutils;

import java.sql.*;

public class DBConnection {

    public static final String DRI = "com.mysql.cj.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/jdbcdemo?&useSSL=false";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "1226xmy0426";

    {
        // 加载驱动
        try {
            Class.forName(DRI);
        } catch (ClassNotFoundException e) {
            // TODO: 2021/3/23
            e.printStackTrace();
        }
    }

    // 获取连接
    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

        return connection;
    }

    // 释放资源
    public static void release(ResultSet rs, PreparedStatement ps, Connection con) throws SQLException {
        if (rs != null) {
            rs.close();
            rs = null;
        }
        if (ps != null) {
            ps.close();
            ps = null;
        }
        if (con != null) {
            con.close();
            con = null;
        }
    }
}
